package com.ipzoe.web.controller.user;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;

import java.util.List;
import java.util.Arrays;

import com.ipzoe.common.utils.SecurityUtils;
import com.ipzoe.mem.domain.Member;
import com.ipzoe.mem.service.IMemberService;
import com.ipzoe.common.utils.StringUtils;
import com.ipzoe.system.service.ISysDictDataService;
import com.ipzoe.web.convert.MemberConvert;
import com.ipzoe.web.model.mem.MemberVO;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ipzoe.common.annotation.Log;
import com.ipzoe.common.core.controller.BaseController;
import com.ipzoe.common.core.domain.AjaxResult;
import com.ipzoe.common.enums.BusinessType;
import com.ipzoe.common.utils.poi.ExcelUtil;
import com.ipzoe.common.core.page.TableDataInfo;

/**
 * 用户Controller
 * 
 * @author eric
 * @date 2020-08-21
 */
@RestController
@RequestMapping("/user/user" )
public class UserController extends BaseController {

    @Autowired
    private IMemberService iMemberService;

    @Autowired
    private ISysDictDataService iSysDictDataService;

    /**
     * 查询用户列表
     */
    @PreAuthorize("@ss.hasPermi('user:user:list')")
    @GetMapping("/list")
    public TableDataInfo list(Member user)
    {
        startPage();
        LambdaQueryWrapper<Member> lqw = new LambdaQueryWrapper<Member>();
        if (StringUtils.isNotBlank(user.getUsername())){
            lqw.like(Member::getUsername ,user.getUsername());
        }
        List<Member> list = iMemberService.list(lqw);
        return getDataTable(list);
    }

    /**
     * 导出用户列表
     */
    @PreAuthorize("@ss.hasPermi('user:user:export')" )
    @Log(title = "用户" , businessType = BusinessType.EXPORT)
    @GetMapping("/export" )
    public AjaxResult export(Member user) {
        LambdaQueryWrapper<Member> lqw = new LambdaQueryWrapper<Member>(user);
        List<Member> list = iMemberService.list(lqw);
        ExcelUtil<Member> util = new ExcelUtil<Member>(Member. class);
        return util.exportExcel(list, "user" );
    }

    /**
     * 获取用户详细信息
     */
    @PreAuthorize("@ss.hasPermi('user:user:query')" )
    @GetMapping(value = "/{id}" )
    public AjaxResult getInfo(@PathVariable("id" ) Long id) {
        return AjaxResult.success(iMemberService.getById(id));
    }

    /**
     * 新增用户
     */
    @PreAuthorize("@ss.hasPermi('user:user:add')" )
    @Log(title = "用户" , businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Member user) {
        return toAjax(iMemberService.save(user) ? 1 : 0);
    }

    /**
     * 修改用户
     */
    @PreAuthorize("@ss.hasPermi('user:user:edit')" )
    @Log(title = "用户" , businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Member user) {
        return toAjax(iMemberService.updateById(user) ? 1 : 0);
    }

    /**
     * 删除用户
     */
    @PreAuthorize("@ss.hasPermi('user:user:remove')" )
    @Log(title = "用户" , businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}" )
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(iMemberService.removeByIds(Arrays.asList(ids)) ? 1 : 0);
    }

    /**
     * 冻结用户
     */
    @PreAuthorize("@ss.hasPermi('user:user:frozen')" )
    @Log(title = "用户" , businessType = BusinessType.UPDATE)
    @PutMapping("/frozen/{id}" )
    public AjaxResult frozen(@PathVariable Long id) {
        Member member = new Member();
        member.setId(id).setStatus(2).setUpdateBy(SecurityUtils.getUsername());
        iMemberService.updateById(member);
        return toAjax(1);
    }

    /**
     * 解冻用户
     */
    @PreAuthorize("@ss.hasPermi('user:user:thaw')" )
    @Log(title = "用户" , businessType = BusinessType.UPDATE)
    @PutMapping("/thaw/{id}" )
    public AjaxResult thaw(@PathVariable Long id) {
        Member member = new Member();
        member.setId(id).setStatus(1).setUpdateBy(SecurityUtils.getUsername());
        iMemberService.updateById(member);
        return toAjax(1);
    }

}
